﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Presentation.Module.Security.Interfaces.Views;
using Presentation.Module.Security.Views;
using Presentation.Shell.Interfaces;
using Microsoft.Practices.Unity;
using Domain.UserManagement;
using Infrastructure.Data.Security;
using Application.Interfaces;
using Application.UserManagement;
using Presentation.Module.Security.ViewModels;
using Presentation.Module.Security.Interfaces.ViewModels;
using Presentation.Module.Security.Service;
using Presentation.Module.Security.Interfaces.Services;
using Infrastructure.CrossCutting.Security;

namespace Presentation.Module.Security
{
    public class UserModule : IPresentationModule
    {
        private readonly IUnityContainer _container;

        public UserModule(IUnityContainer container)
        {
            this._container = container;

            //register repositories
            _container.RegisterType<IUserRepository, UserRepository>();
            _container.RegisterType<IRoleRepository, RoleRepository>();
            _container.RegisterType<IPermissionRepository, PermissionRepository>();

            //register application management for user
            _container.RegisterType<IUserManagementService, UserManagementService>();

            //register user service
            _container.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager());

            //register view models
            _container.RegisterType<IUserViewModel, UserViewModel>(new TransientLifetimeManager());
            _container.RegisterType<IAddUserViewModel, AddUserViewModel>(new TransientLifetimeManager());
            _container.RegisterType<IRoleViewModel, RoleViewModel>(new TransientLifetimeManager());
            _container.RegisterType<IAddRoleViewModel, AddRoleViewModel>(new TransientLifetimeManager());

            //register views
            _container.RegisterType<IUserView, UserView>(new TransientLifetimeManager());
            _container.RegisterType<IAddUserView, AddUserView>(new TransientLifetimeManager());
            _container.RegisterType<IRoleView, RoleView>(new TransientLifetimeManager());
            _container.RegisterType<IAddRoleView, AddRoleView>(new TransientLifetimeManager());

            //_container.AddExtension(new Interception());


            //container.Configure<Interception>()
            //    .SetDefaultInterceptorFor<IUserService>(new TransparentProxyInterceptor())
            //    .AddPolicy("CheckPermissions")
            //    .AddMatchingRule(new TypeMatchingRule(typeof(IUserService)))
            //    .AddCallHandler(typeof(UserManagementCallHandler));                     
        }

        public string Name
        {
            get { return "VPL User Management"; }
        }

        public IModuleLandingPage LandingPage
        {
            get { return this._container.Resolve<UserManagementLandingPage>(); }
        }
    }

}
